package com.cuiweiyou.happypaint.util;

import android.content.Context;
import android.os.Environment;
import android.telephony.TelephonyManager;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

/**
 * Created by macpro on 2017/12/5.
 */
public class PhoneinfoUtil {

    private static TelephonyManager tm;

    private PhoneinfoUtil() {
    }

    private static void getTM(Context ctx) {
        tm = (TelephonyManager) ctx.getSystemService(Context.TELEPHONY_SERVICE);
    }

    /**
     * 基本信息：IMEI号，IESI号，手机型号，品牌，号码
     */
    public static String getBaseinfo(Context ctx) {
        if (null == tm) {
            getTM(ctx);
        }

        String mtyb = android.os.Build.BRAND; // 品牌
        String mtype = android.os.Build.MODEL; // 型号

        return new StringBuilder()//
                .append("品牌:" + mtyb)//
                .append("\n型号:" + mtype)//
                .toString();//
    }

    /**
     * SERIAL Number
     */
    public static String getDeviceSN() {
        String serialNumber = android.os.Build.SERIAL;
        return serialNumber;
    }

    /**
     * 其它信息
     */
    public static String getOtherInfo(Context ctx) {
        if (null == tm) {
            getTM(ctx);
        }

        StringBuilder sb = new StringBuilder();

        sb.append("\nNetworkCountryIso = " + tm.getNetworkCountryIso());
        sb.append("\nNetworkOperator = " + tm.getNetworkOperator());
        sb.append("\nNetworkOperatorName = " + tm.getNetworkOperatorName());
        sb.append("\nPhoneType = " + tm.getPhoneType());
        sb.append("\nSimCountryIso = " + tm.getSimCountryIso());
        sb.append("\nSimOperator = " + tm.getSimOperator());
        sb.append("\nSimOperatorName = " + tm.getSimOperatorName());
        sb.append("\nSimState = " + tm.getSimState());

        return sb.toString();
    }

    /**
     * byte
     */
    public static long getUsableSDCardSpace() {
        File file = Environment.getExternalStorageDirectory();
        long totalSpace = file.getTotalSpace();
        long usableSpace = file.getUsableSpace();
        return usableSpace;
    }

    /**
     * 获得系统总内存 byte
     */
    public static long getTotalMemory() {
        String str1 = "/proc/meminfo";// 系统内存信息文件
        String str2;
        String[] arrayOfString;
        long initial_memory = 0;

        try {
            FileReader localFileReader = new FileReader(str1);
            BufferedReader localBufferedReader = new BufferedReader(localFileReader, 8192);
            str2 = localBufferedReader.readLine();// 读取meminfo第一行，系统总内存大小

            arrayOfString = str2.split("\\s+");

            initial_memory = Integer.valueOf(arrayOfString[1]).intValue() * 1024;
            localBufferedReader.close();

        } catch (IOException e) {
        }

        return initial_memory;// Byte转换为KB或者MB，内存大小规格化
    }

    /**
     * 手机CPU信息。1-cpu型号，2-cpu频率
     */
    public static String[] getCpuInfo() {
        String str1 = "/proc/cpuinfo";
        String str2 = "";
        String[] cpuInfo = {"", ""}; // 1-cpu型号 //2-cpu频率
        String[] arrayOfString;
        try {
            FileReader fr = new FileReader(str1);
            BufferedReader localBufferedReader = new BufferedReader(fr, 8192);
            str2 = localBufferedReader.readLine();
            arrayOfString = str2.split("\\s+");
            for (int i = 2; i < arrayOfString.length; i++) {
                cpuInfo[0] = cpuInfo[0] + arrayOfString[i] + " ";
            }
            str2 = localBufferedReader.readLine();
            arrayOfString = str2.split("\\s+");
            cpuInfo[1] += arrayOfString[2];
            localBufferedReader.close();
        } catch (IOException e) {
        }

        return cpuInfo;
    }


    private static Integer threadPoolSize = null;

    public static int getThreadPoolSize() {
        if (null == threadPoolSize) {
            threadPoolSize = Runtime.getRuntime().availableProcessors() + 1;
        }
        return threadPoolSize;
    }
}
